﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using QMERP.Application.Services;
using QMERP.Domain.Dtos;
using System.Data;
using System.Web;

namespace QMERP.WebApi.Controllers
{
    /// <summary>
    /// 基础接口
    /// </summary>
    [AllowAnonymous]
    public class BasicController : ApiControllerBase
    {
        private readonly IBasicService _basicSvc;
        public BasicController(IBasicService basicSvc)
        {
            _basicSvc = basicSvc;
        }
        /// <summary>
        /// Excel转DataTable
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        /// <exception cref="ApiException"></exception>
        [HttpPost]
        public DataTable GetDataTable(IFormFile? file)
        {
            return _basicSvc.GetDataTable(file);
        }
        /// <summary>
        /// XYExcel转DataTable
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        /// <exception cref="ApiException"></exception>
        [HttpPost]
        public DataTable GetXYTable(IFormFile? file)
        {
            return _basicSvc.GetXYTable(file);
        }
        /// <summary>
        /// XYExcel转LensDetails
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        /// <exception cref="ApiException"></exception>
        [HttpPost]
        public List<LensDetailResponse> GetLensDetails(IFormFile? file)
        {
            return _basicSvc.GetLensDetails(file);
        }
        /// <summary>
        /// 获取XYTable视图
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="ApiException"></exception>
        [HttpPost]
        public async Task<XYTableViewResponse> GetXYTableView(XYTableViewRequest request)
        { 
            return await _basicSvc.GetXYTableView(request);
        }
        /// <summary>
        /// 导出XYTableExcel
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="ApiException"></exception>
        [HttpPost]
        public FileResult DownLoadXYTableExcel(DownLoadXYTableExcelRequest request)
        {
            MemoryStream stream = _basicSvc.DownLoadXYTableExcel(request);
            string fileName = request.Title + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xlsx";
            fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
            return File(stream.ToArray(), "application/octet-stream", fileName);
        }
        ///// <summary>
        ///// 获取出入库记录原因
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //public List<ComboBoxResponse> GetActionLogReasons(int? type)
        //{
        //    if (type == (int)StockActionTypeEnum.In)
        //        return _commonSvc.GetComboBoxList<StockActionLogReasonInEnum>();
        //    else if (type == (int)StockActionTypeEnum.Out)
        //        return _commonSvc.GetComboBoxList<ActionLogReasonOutEnum>();
        //    else
        //    {
        //        var list1 = _commonSvc.GetComboBoxList<StockActionLogReasonInEnum>();
        //        var list2 = _commonSvc.GetComboBoxList<ActionLogReasonOutEnum>();
        //        return list1.Concat(list2).ToList();
        //    }
        //}
        /// <summary>
        /// 获取出入库记录状态
        /// </summary>
        /// <returns></returns>
        //[HttpGet]
        //public List<ComboBoxResponse> GetActionLogStatuses()
        //{
        //    return _commonSvc.GetComboBoxList<StockActionLogStatusEnum>();
        //}
    }
}
